﻿using HandyControl.Controls;
using MG.Util.Log;

namespace MG.SmartHMI.Common
{
    public class EventBus
    {
        private EventBus()
        {
            _LogServer = Logger.Defalut;
        }

        private static EventBus _Default { get; set; }

        public static EventBus Default
        {
            get
            {
                if (_Default == null)
                    _Default = new EventBus();
                return _Default;
            }
        }

        #region Log

        private Logger _LogServer;
        private Action<LogInfo> LogAction { get; set; }
        private Action<LogInfo> DebugLogAction { get; set; }
        private Action<LogInfo> SlnLogAction { get; set; }

        public void BindLogAction(Action<LogInfo> action) => LogAction += action;

        public void BindDebugLogAction(Action<LogInfo> action) => DebugLogAction += action;

        public void BindSlnLogAction(Action<LogInfo> action) => SlnLogAction += action;

        public void Log(LogInfo log)
        {
            LogAction?.Invoke(log);
            _LogServer?.Log(log);
        }

        public void DebugLog(LogInfo log)
        {
            if (log.LogLevel < LogLevel.Trace)
                DebugLogAction?.Invoke(log);
            _LogServer?.Log(log);
        }

        public void SlnLog(LogInfo log)
        {
            if (log.LogLevel < LogLevel.Trace)
                SlnLogAction?.Invoke(log);
            _LogServer?.Log(log);
        }

        #endregion Log

        #region Pop

        public void PopUpSuccess(string message) => Growl.Success(message);

        public void PopUpWarning(string message) => Growl.Warning(message);

        public void PopUpError(string message) => Growl.Error(message);

        #endregion Pop

        #region CircleTimeTrigger

        private Action CircleTimeAction { get; set; }

        public void BindCircleTimeAction(Action action)
        {
            CircleTimeAction += action;
        }

        public void UnBindCircleTimeAction(Action action)
        {
            if (CircleTimeAction != null && CircleTimeAction.GetInvocationList().Contains(action))
            {
                CircleTimeAction -= action;
            }
        }

        public void CircleTimeTrigger()
        {
            CircleTimeAction?.Invoke();
        }

        #endregion CircleTimeTrigger
    }
}